package cn.liccshop.core.service.shopTransaction.service;

import cn.liccshop.core.entity.ShopTransaction;
import cn.liccshop.core.service.shopTransaction.mapper.ShopTransactionMapper;
import cn.liccshop.core.service.shopTransaction.service.IShopTransactionService;
import cn.liccshop.sys.exception.MySystemException;
import cn.liccshop.sys.zhifubao.MyPay;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 交易信息表 服务实现类
 * </p>
 *
 * @author LiCC
 * @since 2019-05-26
 */
@Service
public class ShopTransactionServiceImpl extends ServiceImpl<ShopTransactionMapper, ShopTransaction> implements IShopTransactionService {

    @Autowired
    private MyPay myPay;

    @Override
    public Map<String, Object> transactionVChartsInfoBySex() {
        /** 获取分类信息 **/
        List<Map<String, Object>> infosOfDB = baseMapper.transactionVChartsInfoBySex();
        /** 存储列属性 **/
        List<String> columns = new ArrayList<>();
        columns.add("性别");
        columns.add("消费额");
        /** 存储数据 **/
        List<Map<String, Object>> rows = new ArrayList<>();
        Map<String, Object> secret = new HashMap<>();
        secret.put(columns.get(0), "保密");
        Map<String, Object> men = new HashMap<>();
        men.put(columns.get(0), "男");
        Map<String, Object> women = new HashMap<>();
        women.put(columns.get(0), "女");
        BigDecimal allNum = new BigDecimal("0.00");
        if (CollectionUtils.isNotEmpty(infosOfDB)){
            for(Map<String, Object> sexInfo : infosOfDB){
                if (0 == (Integer) sexInfo.get("sex")){
                    secret.put(columns.get(1), sexInfo.get("amount"));
                    allNum = allNum.add((BigDecimal) sexInfo.get("amount"));
                } else if (1 == (Integer) sexInfo.get("sex")){
                    men.put(columns.get(1), sexInfo.get("amount"));
                    allNum = allNum.add((BigDecimal) sexInfo.get("amount"));
                } else if (2 == (Integer) sexInfo.get("sex")){
                    women.put(columns.get(1), sexInfo.get("amount"));
                    allNum = allNum.add((BigDecimal) sexInfo.get("amount"));
                }
            }
        }
        if (secret.get(columns.get(1))==null){
            secret.put(columns.get(1), 0);
        }
        if (men.get(columns.get(1))==null){
            men.put(columns.get(1), 0);
        }
        if (women.get(columns.get(1))==null){
            women.put(columns.get(1), 0);
        }
        rows.add(secret);
        rows.add(men);
        rows.add(women);
        Map<String, Object> data = new HashMap<>();
        data.put("columns", columns);
        data.put("allNum", allNum);
        data.put("rows", rows);
        return data;
    }

    @Override
    public Map<String, Object> transactionReturnById(String id) throws MySystemException {
        ShopTransaction transaction = baseMapper.selectById(id);
        Map<String, Object> result = myPay.refund(transaction.getOrderSn(),transaction.getAmount().toString());
        if ((Integer) result.get("code") == 1){
            baseMapper.updateById(new ShopTransaction().setId(id).setType(3).setReturnTime(LocalDateTime.now()));
        }
        return result;
    }
}
